package com.cn.juc;

import java.util.concurrent.*;

/**
 * @author he_ls
 * @since 2024/10/31 17:04
 **/
public class T01_CreateThread {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 5th threadPool
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(new MyRun());
        Future<?> f1 = service.submit(new MyRun());
        Future<String> f2 = service.submit(new MyCall());
        Object o = f2.get();
        System.out.println(o);
        System.out.println("f1 : " + f1.get());
        service.shutdown();

        // 4th lambda
        Thread t4 = new Thread(() -> {
            System.out.println("4、hello lambda !!!");
        });
        t4.start();

        // 3rd
        FutureTask<String> ft = new FutureTask<>(new MyCall());
        Thread t3 = new Thread(ft);
        t3.start();
        // 这个方法是ft.get()是阻塞的
        System.out.println("value of 3rd: " + ft.get());

        // 2nd
        MyThread t2 = new MyThread();
        t2.start();

        // 1st
        Thread t1 = new Thread(new MyRun());
        System.out.println(t1.getState());
        t1.start();
        System.out.println(t1.getState());
        System.out.println(t1.getState());
    }

    /**
     * 创建线程的5个方式
     */
    static class MyRun implements Runnable {
        @Override
        public void run() {
            System.out.println("1、hello runnable!!");
        }
    }

    static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println("2、hello thread!!");
        }
    }

    static class MyCall implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("3、hello myCall !!");
            return "success";
        }
    }
}
